package com.example.everying.framework.websocket.handler;

import com.alibaba.fastjson2.JSONObject;
import com.example.everying.constant.ChatMessageConstant;
import com.example.everying.framework.websocket.service.WebSocketChatService;
import com.example.everying.model.dto.chat.ChatMessagePageDto;
import com.example.everying.model.vo.chat.ChatCommentMessageVo;
import com.example.everying.service.chat.ChatMessagesService;
import jakarta.websocket.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class CommentListMessageHandler implements MessageHandler{

    private static ChatMessagesService chatMessagesService;
    @Autowired
    private void setService(ChatMessagesService chatMessagesService) {
        CommentListMessageHandler.chatMessagesService = chatMessagesService;
    }
    /**
     * @param session
     * @param message
     */
    @Override
    public void handle(Session session, JSONObject message) {
        String userId = WebSocketChatService.getUserId(session);
        ChatMessagePageDto query = new ChatMessagePageDto();
        query.setUserId(userId);
        query.setPage(message.getInteger("page"));
        query.setLimit(message.getInteger("limit"));
        query.setMessagesType(ChatMessageConstant.MESSAGE_TYPE_ACTIVITY_COMMENT);
        ChatCommentMessageVo chatCommentMessageVo = chatMessagesService.getCommentMessageList(query);
        JSONObject result = new JSONObject();
        result.put("type", "commentList");
        result.put("commentList", chatCommentMessageVo.getChatMessageVos());
        WebSocketChatService.sendMessageToUserId(userId,result.toJSONString());

    }
}
